/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.CustomerIOTRPortal;

import session.TX.DishManagerSBRemote;
import session.TX.OutletSBRemote;
import session.TX.StakeholderAccountsSBRemote;
import helper.tx.Helper_FoodItemSearch;
import helper.tx.Helper_Outlet;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "searchManagedBean")
@ViewScoped
public class MealSearchMB {

    @EJB
    private DishManagerSBRemote dishBean;
    @EJB
    private OutletSBRemote outletBean;
    @EJB
    private StakeholderAccountsSBRemote stakeholderBean;
    @ManagedProperty(value = "#{FoodOrderManagerBean}")
    private FoodOrderMB foodOrderMB;
    private String mealName;
    private String outletStr;
    private String mealType;
    private List<Helper_Outlet> outlets;
    private List<Helper_Outlet> suggestionOutlets;
   

    /** Creates a new instance of SearchMB */
    public MealSearchMB() {
    }

    public List<Helper_FoodItemSearch> searchFoodItemsForSale() {
        if (mealName == null) {
            return null;
        }
        List<Helper_FoodItemSearch> returnList = dishBean.searchFoodItemHelperByName(mealName);
        
        return returnList;
    }

    public List<Helper_Outlet> getAllOutlets() {
        return outletBean.getAllOutletsHelper();
    }

    public List<Helper_Outlet> completeOutletSelection(String query) {
        if (outlets == null) {
            outlets = getAllOutlets();
        }
        suggestionOutlets = new ArrayList<Helper_Outlet>();
        for (Helper_Outlet o : outlets) {
            if (o.getStakeholderName().toLowerCase().contains(query.toLowerCase())) {
                suggestionOutlets.add(o);
            }
        }
        return suggestionOutlets;
    }

    public void updateOutletId(ActionEvent event) {
        System.out.println("im here!!!!");
        System.out.println("wtf!!");
        System.out.println(outletStr.equals(""));
        String[] select = outletStr.split(",");
        System.out.println(select.length);
        Helper_Outlet out;
        for (Helper_Outlet o : suggestionOutlets) {
            if (o.getStakeholderName().trim().equalsIgnoreCase(select[0].trim())
                    && o.getOutletName().trim().equalsIgnoreCase(select[1].trim())) {
                System.out.println("changing..");
                foodOrderMB.setOutletId(o.getId());
            }
        }
    }

    public void updateOutletFood(ActionEvent event) {
        System.out.println("outletStr: " + outletStr);

        if (outletStr.split(",").length < 2) {
            return;
        }

        foodOrderMB.setOutletId(outletBean.searchOutletIdByStr(outletStr.split(",")));
    }

    public String getMealName() {
        return mealName;
    }

    public void setMealName(String mealName) {
        this.mealName = mealName;
    }

    public String getOutletStr() {
        return outletStr;
    }

    public void setOutletStr(String outletStr) {
        this.outletStr = outletStr;
    }

    public String getMealType() {
        return mealType;
    }

    public void setMealType(String mealType) {
        System.out.println("setting meal type......" + mealType);
        this.mealType = mealType;
    }

    public FoodOrderMB getFoodOrderMB() {
        return foodOrderMB;
    }

    public void setFoodOrderMB(FoodOrderMB foodOrderMB) {
        this.foodOrderMB = foodOrderMB;
    }
    
}